<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Cashs extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        self::checktoken($data['id']);
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $rows = $data['listrow'] : $rows = 20;
        $res = \db('home_cashs')
            ->where('uid',$data['id'])
            ->paginate($rows,true,['page'=>$page]);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 申请提现
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function applyCash(Request $request)
    {
        $data = $request->param();
        self::checktoken($data['id']);
        $info = \db('home_users')->where('id',$data['id'])->field('role,alipaynum,realname,rd_coin')->find();
        if ($info['role'] == 1){
            return apistatus(0,'您没有提现权限');
        }elseif(empty($info['alipaynum']) || empty($info['realname'])){
            return apistatus(0,'请先完善您的提现信息');
        }
        $cashrd_coin = config('base.web_lowcashcoin');
        if ($data['rd_coin'] < $cashrd_coin){
            return apistatus(0,"最低提现红豆币为{$cashrd_coin}");
        }
        $insert = [
            'uid'=>$data['id'],
            'rd_coin'=>$data['rd_coin'],
            'ordersn'=> 'TX'.date('YmdHis').rand(10000,99999),
            'money'=>$data['rd_coin'],
            'create_time'=>time(),
            'status'=>1,
        ];
        $id = \db('home_cashs')->insertGetId($insert);
        $update = [
            'id'=>$data['id'],
            'rd_coin'=>bcsub($info['rd_coin'],$data['rd_coin'],2),
        ];
        $coin = \db('home_users')->update($update);
        if ($id && $coin){
            return apistatus(1,'申请成功请等待审核');
        }else{
            return apistatus(0,'申请失败请重试');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    

}
